<% content_for :css do %>
  <%= stylesheet_link_tag 'customers_index' %>
<% end %>
<% content_for :title do %>
   <%= params[:filter] ? t(params[:filter]) : '所有的联系人' %>
<% end %>
<div class="grid_18 customers">
  <div class="grid_18 head alpha omega">
    <div class="title">
    <% if @tag.present? %>
      带有『<%= @tag.name %>』标签的联系人<small>(共<%= @customers.size %>条)  <%= link_to "◀", customers_url, :title => '点击看所有联系人' %></small>
    <% elsif params[:untagged].present? %>
      未带有标签的联系人<small>(共<%= @customers.size %>条)  <%= link_to "◀", customers_url, :title => '点击看所有联系人' %></small>
    <% elsif params[:being_assinged].present? %>    
      泛销售推荐的联系人<small>(共<%= @customers.size %>条)  <%= link_to "◀", customers_url, :title => '点击看所有联系人' %></small>
    <% else %>
      <%= params[:filter] ? t(params[:filter]) : '所有的联系人' %><small>(共<%= @customers.size %>条)</small>
    <% end %>
    </div>
  </div>
  <div class="grid_12 alpha">
    <div class="grid_12 alpha all_customers">
      <div class="grid_12 alpha tools">
        <div class='left'>
          <%= link_to_function "全选","", :id => :select_all  %><b>|</b>
          <%= link_to_function "反选","", :id => :i_select_all  %><b>|</b>
          <%= link_to_function "取消","", :id => :un_select_all  %><b>|</b>
          <small class='tip'>选中后可对其加标签,删除操作</small>
          <%= link_to_function "加标签","", :id => 'add_tags', :title => '对已选中的联系人添加标签', :class => 'hidden un_selected'  %><b class='un_selected hidden'>|</b>
          <% if @tag.present? %>
          <%= link_to_function "删标签","", :id => 'remove_tags', :title => "对已选中的联系人删除标签『#{@tag.name}』", :class => 'hidden un_selected'  %><b class='un_selected hidden'>|</b>
          <% end %>
          <%= link_to_function "删除","", :id => 'destroy', :title => '删除以选中的联系人资料' , :class => 'hidden un_selected'  %>
        </div>
        <script type="text/javascript">
          $(function(){
            $('#select_all').click(function(){
              $('.customer_id').attr('checked',true);
              $('.tip').hide();
              $('.un_selected').show();
              $customer_id_checked();
            });

            $('#un_select_all').click(function(){
              $('.customer_id').attr('checked',false);
              $('.tip').show();
              $('.un_selected').hide();
              $customer_id_checked();
            });

            $('#i_select_all').click(function(){
              $('.customer_id').each(function() {
                $(this).attr('checked',!$(this).attr('checked'));
              })
              $customer_id_checked();
            });

            $('.customer_id').click(function() {
              $customer_id_checked();
            });  

            $('#add_tags').click(function(){
              $.get(
                '<%= new_tag_url %>?' + $('#customers_form').serialize()
              );
            });

            $('#destroy').click(function() {
              if(confirm('删除后这些联系人资料将无法恢复了，确认吗？')){
                $.post(
                  '<%= blank_out_some_customers_url  %>', {
                    _method:'delete',
                    <% if @tag.present? %>
                    'id[]': $checked_customer_ids(),
                    tag_id: <%= @tag.id %>
                    <% else %>
                    'id[]': $checked_customer_ids()
                    <% end %>
                  }
                );  
              }
            });

            <% if @tag.present? %>
            $('#remove_tags').click(function() {
              if(confirm('即将从已选中的联系人上移除标签『<%= @tag.name %>』，确认吗？')) {
                $.post(
                  '<%= unbundle_one_tags_url %>', {
                    _method:'delete',
                    'customer_id[]': $checked_customer_ids(),
                    tag_id: <%= @tag.id %>
                  }
                );
              }
            });
            <% end %>

            $checked_customer_ids = function() {
              var json_str = '[';
              $('#customers_form .customer_id:checked').each(function(){
                json_str += $(this).val() + ','
              });
              json_str += ']';
              return eval(json_str);
            }

            $customer_id_checked = function() {
            //   console_debug($(".customer_id:checked").length);
              if($(".customer_id:checked").length > 0) {                
                $('.tip').hide();
                $('.un_selected').show(); 
                $('#add_tags').html('▶加标签(<b>' + $(".customer_id:checked").length + '</b>)');
                $('#destroy').html('▷删除(<b>' + $(".customer_id:checked").length + '</b>)');
                <% if @tag.present? %>
                $('#remove_tags').html('▶删标签(<b>' + $(".customer_id:checked").length + '</b>)');
                <% end %>
                $('.customers .tools .right').hide();
              } else {
                $('.tip').show();
                $('.un_selected').hide();
                $('.customers .tools .right').show();
              }
            };
          })  
          
        </script>
        <div class="right">
          <%#= render :partial => 'search.html' %>
        </div>
      </div>
      <div id='add_tags_form' class='grid_12 alpha hidden'>
        
      </div>
      <div id="customers_show">
        <%= render :partial => 'show_4_ajax.html', :locals => {:customers => @customers} %>
      </div>
    </div>
  </div>
  <div class="grid_5 omega tags">
    <div id="quick_search" class="block grid_5 alpha omega">
      <%= render :partial => 'autocomplete_by_key_words.html' %>
      <div class="grid_5 alpha">
        <%= link_to '增加一位联系人资料',new_customer_url, :class => :new_button, :target => :_blank %>
        <%#= link_to '增加一项任务',tasks_url, :class => :new_button, :target => :_blank %>
      </div>
    </div>    
    <div id='right_tags' class="block grid_5 alpha omega">
      <%= render :partial => 'right_tags.html' %>
    </div>
  </div>
</div>
<script type="text/javascript">
  $(function(){
    $('.customer_id').attr('checked',false);
  })
</script>